Conditions | 1 |
Paths | 4 |
Total Lines | 70 |
Code Lines | 51 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** |
||
43 | return new Promise((fulfill, reject) => { |
||
44 | // Perform initial load |
||
45 | $.ajax(config.url, { |
||
46 | method: config.method, |
||
47 | data: { |
||
48 | _dt: config.name, |
||
49 | _init: true |
||
50 | } |
||
51 | }).done(function(data) { |
||
52 | var baseState; |
||
53 | |||
54 | // Merge all options from different sources together and add the Ajax loader |
||
55 | var dtOpts = $.extend({}, data.options, config.options, options, persistOptions, { |
||
56 | ajax: function (request, drawCallback, settings) { |
||
57 | if (data) { |
||
58 | data.draw = request.draw; |
||
59 | drawCallback(data); |
||
60 | data = null; |
||
61 | if (Object.keys(state).length && dt.state != null) { |
||
62 | var merged = $.extend(true, {}, dt.state(), state); |
||
63 | dt |
||
64 | .order(merged.order) |
||
65 | .search(merged.search.search) |
||
66 | .page.len(merged.length) |
||
67 | .page(merged.start / merged.length) |
||
68 | .draw(false); |
||
69 | } |
||
70 | } else { |
||
71 | request._dt = config.name; |
||
72 | $.ajax(config.url, { |
||
73 | method: config.method, |
||
74 | data: request |
||
75 | }).done(function(data) { |
||
76 | drawCallback(data); |
||
77 | }) |
||
78 | } |
||
79 | } |
||
80 | }); |
||
81 | |||
82 | root.html(data.template); |
||
83 | dt = $('table', root).DataTable(dtOpts); |
||
84 | if (config.state !== 'none') { |
||
85 | dt.on('draw.dt', function(e) { |
||
86 | var data = $.param(dt.state()).split('&'); |
||
87 | |||
88 | // First draw establishes state, subsequent draws run diff on the first |
||
89 | if (!baseState) { |
||
90 | baseState = data; |
||
91 | } else { |
||
92 | var diff = data.filter(el => { return baseState.indexOf(el) === -1 && el.indexOf('time=') !== 0; }); |
||
93 | switch (config.state) { |
||
94 | case 'fragment': |
||
95 | history.replaceState(null, null, window.location.origin + window.location.pathname + window.location.search |
||
96 | + '#' + decodeURIComponent(diff.join('&'))); |
||
97 | break; |
||
98 | case 'query': |
||
99 | history.replaceState(null, null, window.location.origin + window.location.pathname |
||
100 | + '?' + decodeURIComponent(diff.join('&') + window.location.hash)); |
||
101 | break; |
||
102 | } |
||
103 | } |
||
104 | }) |
||
105 | } |
||
106 | |||
107 | fulfill(dt); |
||
108 | }).fail(function(xhr, cause, msg) { |
||
109 | console.error('DataTables request failed: ' + msg); |
||
110 | reject(cause); |
||
111 | }); |
||
112 | }); |
||
113 | }; |
||
185 |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.